﻿using Yxinmt.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Yxinmt.Utility;

namespace Yxinmt.Windows
{
    public class MainWindow
    {
        public IWindow CurrentWindow { get; set; }
        public IWindow ParentWindow { get; set; }
        public System.Windows.Window Window { get; set; }
        public static Type WindowType { get; set; }
        public System.Windows.Controls.DockPanel PagePanel { get; set; }
        public bool IsDialog { get; set; }
        public string Name { get; set; }
        public Dictionary<string, object> Parameters { get; set; }
        public Entity.ClientConfig ClientConfig { get; set; }

        public Action ExitAction { get; set; }

        public MainWindow(System.Windows.Window window, string name, Type windowType, Dictionary<string, object> clientConfig, Action<Action> initEventAction)
        {
            Window = window;
            WindowType = windowType;
            ClientConfig = new Entity.ClientConfig();
            ClientConfig.Serial = clientConfig.GetStringValue("Serial");
            ClientConfig.Mac = clientConfig.GetStringValue("Mac");
            ClientConfig.ServiceUrl = clientConfig.GetStringValue("ServiceUrl");

            ViewConfig.LoadViewConfig();

            Name = string.IsNullOrEmpty(name) ? "Index" : name;
            InitWindow(true);

            window.Closing += Window_Closing;

            initEventAction(() =>
            {
                if (ExitAction != null) ExitAction();
            });
        }

        public MainWindow(System.Windows.Window window, string name, IWindow parentForm, bool isDialog, bool blInit, Dictionary<string, object> parameters = null)
        {
            Window = window;
            ParentWindow = parentForm;
            IsDialog = isDialog;
            Name = name;
            Parameters = parameters;
            InitWindow(blInit);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //Code.Controls.VlcPlayer.ClosePlayerList();
        }

        public MainWindow(System.Windows.Window window, string name, IWindow parentWindow, Dictionary<string, object> parameters = null)
        {
            Window = window;
            ParentWindow = parentWindow;
            Name = name;
            Parameters = parameters;
            InitWindow(true);
        }

        public MainWindow(System.Windows.Window window, string name, Dictionary<string, object> parameters = null)
        {
            Window = window;
            Name = name;
            Parameters = parameters;
            InitWindow(true);
        }

        private void InitWindow(bool blInit)
        {
            try
            {
                this.CurrentWindow = this.GetWindow();

                if (blInit)
                {
                    this.CurrentWindow.GetElements().ForEach(c => this.CurrentWindow.Grid.Children.Add(c));

                    this.CurrentWindow.EventLoad();
                    this.CurrentWindow.DataLoadAsync();
                }
            }
            catch (Exception ex)
            {
                Utility.Common.Alert("MainWindow", "InitWindow", ex);
            }
        }

        void Alert(string entityName, string methodName, Exception ex)
        {
            if (this.CurrentWindow != null) this.CurrentWindow.Alert(entityName, methodName, ex);
            else if (this.ParentWindow != null) this.ParentWindow.Alert(entityName, methodName, ex);
            else Utility.Common.Alert(entityName, methodName, ex);
        }

        IWindow GetWindow()
        {
            switch (Name)
            {
                case "Default": return new DefaultPage(Name, this.Window, Parameters);
                case "AlertConfrim": return new AlertConfrim(this.Window, ParentWindow, IsDialog, Parameters);
                default: return GetWindowFunc(this);
            }
        }

        public static Func<MainWindow, IWindow> GetWindowFunc { get; set; }

        public void ShowDialog()
        {
            ShowDialog((r) => { });
        }

        public void ShowDialog(Action callback)
        {
            ShowDialog((r) => { if (r && callback != null) callback(); });
        }

        public void ShowDialog(Action<bool> callback)
        {
            Task.Run(() =>
            {
                Window.Dispatcher.Invoke(() =>
                {
                    try
                    {
                        bool? result = Window.ShowDialog();
                        if (callback != null) callback(result != null && result.Value);
                    }
                    catch (Exception ex)
                    {
                        Alert("MainWindow", "ShowDialog", ex);
                    }
                });
            });
        }
    }
}
